﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Web;
using System.IO;
using System.Threading;
using Newtonsoft.Json.Linq;
using blackhouse.BlackApart.Business.Opinions;
using System.Globalization;
using blackhouse.BlackApart.Business.PushMails;
using blackhouse.Web;
using blackhouse.BlackApart.Business.Newsletter;
using blackhouse.AsyncMail.Business;
using blackhouse.AsyncMail.Business.Data;
using blackhouse.Aparts.Web.Presenter;
using mojoPortal.Business.WebHelpers;
using blackhouse.BlackApart.Web.Modules;
using blackhouse.Currencies;

namespace blackhouse.Aparts.Web.Handlers {
    /// <summary>
    /// Summary description for ApartsService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class BlackApartService : System.Web.Services.WebService {

        private Factory factory = Factory.Instance();

        [WebMethod]
        public blackhouse.Aparts.Web.View.DisplayOpinion[] GetOpinions(int lastLoadedOpinion) {
            bool loadAll = WebUser.IsAdminOrContentAdminOrContentPublisher || WebUser.IsInRole("Opinions");
            var opinions = (from o in this.factory.GetOpinion().GetOpinions(loadAll) orderby o.PublishedDate descending select o)
                .SkipWhile(o => o.Id != lastLoadedOpinion).Skip(1)
                .Take(OpinionsPresenter.OPINION_PART_COUNT);
            return opinions.Select(o => new blackhouse.Aparts.Web.View.DisplayOpinion(o)).ToArray();
        }

        [WebMethod]
        public blackhouse.Aparts.Web.View.DisplayOpinion[] GetSimilarOpinions(int lastLoadedOpinion, int opinionId) {
            var opinions = (from o in this.factory.GetOpinion().GetSimilarOpinions(opinionId, TimeSpan.FromDays(90)) orderby o.PublishedDate descending select o);
            var lOpinions = opinions.ToList();
            if (lastLoadedOpinion > 0)
                lOpinions = opinions.SkipWhile(o => o.Id != lastLoadedOpinion).Skip(1).ToList();
            return lOpinions.Take(OpinionsPresenter.OPINION_PART_COUNT).Select(o => new blackhouse.Aparts.Web.View.DisplayOpinion(o)).ToArray();
        }

        [WebMethod]
        public ApartmentInfo[] GetAllApartments() {
            return ApartmentInfo.GetAll();
        }

        [WebMethod]
        public string[] CurrencyToString(double[] values) {
            DisplayCurrency c = new DisplayCurrency();
            Currency currency = new Currency(0, c.GetCurrentCurrency(), 2);
            string[] converted = new string[values.Length];
            for (int xv = 0; xv < values.Length; xv++) {
                currency.Value = values[xv];
                converted[xv] = currency.ToString();
            }
            return converted;
        }

    }
}
